package com.heer.gymnasium.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.heer.gymnasium.R;
import com.heer.gymnasium.activity.MainActivity;
import com.heer.gymnasium.dialog.QRAccessDialog;
import com.heer.gymnasium.util.LoadImageUtil;
import com.heer.model.MyCardBean;

import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by rzw2 on 2017/4/5.
 */

public class MyCardAdapter extends ListAdapter<MyCardBean> {
    public MyCardAdapter(Context context) {
        super(context);
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        ViewHolder holder;
        if (view != null) {
            holder = (ViewHolder) view.getTag();
        } else {
            view = mInflater.inflate(R.layout.item_membership_card, parent, false);
            holder = new ViewHolder(view);
            view.setTag(holder);
        }

        final MyCardBean item = getItem(position);

        holder.tvTitle.setText(item.getTicket_name() + "(" + item.getCtaname() + ")");
        holder.tvSite.setText("使用场地："+item.getPlace_name());
        Glide.with(mContext)
                .load(LoadImageUtil.getLoadImageUrl(item.getImage()))
                .placeholder(R.mipmap.ic_order_card)
                .into(holder.imageCard);
      /*  SimpleDateFormat sdf = new SimpleDateFormat("yy年MM月dd日");
        if (TextUtils.equals(item.getUsed(), "0")) {
            Date endTime = new Date(Long.valueOf(item.getFinish_time()) * 1000+ Long.valueOf(item.getExpire())*86400000);
            holder.tvStatus.setText("有效期至$1前".replace("$1",sdf.format(endTime)));
        } else*/
      if (!TextUtils.equals(item.getUse_num(), "0")) {
            holder.tvStatus.setText("已使用"+item.getUse_num()+"次");
        } else if (TextUtils.equals(item.getUsed(), "2")) {
            holder.tvStatus.setText("已过期");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        Date endTime = new Date(Long.valueOf(item.getStime()) * 1000);
        holder.tv_time.setText("有效期："+sdf.format(endTime));

        holder.btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //new QRAccessDialog(mContext, item.getOrder_sn()).show();
                Intent intent=new Intent(mContext, MainActivity.class);
                intent.putExtra("index",2);
                mContext.startActivity(intent);
                ((Activity)mContext).finish();
            }
        });

        return view;
    }

    static class ViewHolder {
        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.image_card)
        ImageView imageCard;
        @BindView(R.id.tv_site)
        TextView tvSite;
        @BindView(R.id.tv_status)
        TextView tvStatus;
        @BindView(R.id.layout_card)
        LinearLayout layoutCard;
        @BindView(R.id.btn_2)
        TextView btn2;
        @BindView(R.id.layout_item)
        LinearLayout layoutItem;
        @BindView(R.id.tv_member_time)
        TextView tv_time;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
